//
//  RSAutolayoutTextViewCell.m
//  RSTools
//
//  Created by thinkjoy on 2017/9/15.
//  Copyright © 2017年 杜瑞胜. All rights reserved.
//

#import "RSAutolayoutTextViewCell.h"
#import <IQKeyboardManager/IQKeyboardManager.h>

#import "BRPlaceholderTextView.h"


@interface RSAutolayoutTextViewCell ()

@property (weak, nonatomic) IBOutlet BRPlaceholderTextView *mainTextView;

/**
 mainTextView最大高度的约束值
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *mainTextV_MaxHeight;

@end

@implementation RSAutolayoutTextViewCell

+ (instancetype)getRSAutolayoutTextViewCellWithTableView:(UITableView *)tableView
{
    RSAutolayoutTextViewCell * cell;
    if (TextViewMaxHeight==0 || TextViewMaxHeight==MAXFLOAT) {
        static NSString * cellID    =   @"RSAutolayoutTextViewCell";
        
        cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([self class])];
        if (!cell) {
            [tableView registerNib:[UINib nibWithNibName:cellID bundle:nil] forCellReuseIdentifier:cellID];
            cell    =   [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
        }
    }else{
        cell    =   [[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil].firstObject;
    }
    
    cell.selectionStyle =   UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.mainTextV_MaxHeight.constant   =   TextViewMaxHeight;
    // Initialization code
}

- (void)setInfoDic:(NSDictionary *)infoDic
{
    _infoDic    =   infoDic;
    self.mainTextView.text  =   infoDic[TableViewTextKey];
    
    self.mainTextView.placeholder   =   infoDic[TableViewPlaceholderKey];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)textViewDidChange:(UITextView *)textView
{
    if ([self.delegate respondsToSelector:@selector(rsAutolayoutTextViewCell:didChangeText:)]) {
        [self.delegate rsAutolayoutTextViewCell:self didChangeText:textView.text];
    }
    
    if (self.didChangeTextBlock) {
        self.didChangeTextBlock(textView.text);
    }
    
    [[IQKeyboardManager sharedManager] reloadLayoutIfNeeded];
    CGRect bounds = textView.bounds;
    // 计算 text view 的高度
    CGSize maxSize = CGSizeMake(bounds.size.width, CGFLOAT_MAX);
    CGSize newSize = [textView sizeThatFits:maxSize];
    bounds.size = newSize;
    textView.bounds = bounds;
    // 让 table view 重新计算高度
    UITableView *tableView = [self tableView];
    [tableView beginUpdates];
    [tableView endUpdates];
    
    
    if (textView.frame.size.height>=self.mainTextV_MaxHeight.constant) {
        [textView setScrollEnabled:YES];
    }else{
        [textView setScrollEnabled:NO];
    }
    
}
- (UITableView *)tableView
{
    UIView *tableView = self.superview;
    while (![tableView isKindOfClass:[UITableView class]] && tableView) {
        tableView = tableView.superview;
    }
    return (UITableView *)tableView;
}

@end
